package cn.com.anypay.manager.common.exception;

import cn.com.anypay.manager.common.enums.ResultCode;

/**
 * 微信认证异常类
 * 用于处理微信认证过程中的各种异常情况
 */
public class WeChatAuthException extends BusinessException {
    
    /**
     * 微信错误码
     */
    private final String weChatErrorCode;
    
    /**
     * 微信错误描述
     */
    private final String weChatErrorMsg;
    
    public WeChatAuthException(String message) {
        super(ResultCode.AUTHENTICATION_FAILED, message);
        this.weChatErrorCode = null;
        this.weChatErrorMsg = null;
    }
    
    public WeChatAuthException(String message, Throwable cause) {
        super(ResultCode.AUTHENTICATION_FAILED, message, cause);
        this.weChatErrorCode = null;
        this.weChatErrorMsg = null;
    }
    
    public WeChatAuthException(String weChatErrorCode, String weChatErrorMsg, String message) {
        super(ResultCode.AUTHENTICATION_FAILED, message);
        this.weChatErrorCode = weChatErrorCode;
        this.weChatErrorMsg = weChatErrorMsg;
    }
    
    public WeChatAuthException(String weChatErrorCode, String weChatErrorMsg, String message, Throwable cause) {
        super(ResultCode.AUTHENTICATION_FAILED, message, cause);
        this.weChatErrorCode = weChatErrorCode;
        this.weChatErrorMsg = weChatErrorMsg;
    }
    
    public String getWeChatErrorCode() {
        return weChatErrorCode;
    }
    
    public String getWeChatErrorMsg() {
        return weChatErrorMsg;
    }
    
    /**
     * 创建无效授权码异常
     */
    public static WeChatAuthException invalidCode() {
        return new WeChatAuthException("40029", "invalid code", "微信授权码无效或已过期");
    }
    
    /**
     * 创建授权码已过期异常
     */
    public static WeChatAuthException codeExpired() {
        return new WeChatAuthException("40030", "code expired", "微信授权码已过期");
    }
    
    /**
     * 创建访问令牌已过期异常
     */
    public static WeChatAuthException accessTokenExpired() {
        return new WeChatAuthException("40001", "access token expired", "微信访问令牌已过期");
    }
    
    /**
     * 创建OpenID无效异常
     */
    public static WeChatAuthException invalidOpenId() {
        return new WeChatAuthException("40003", "invalid openid", "微信OpenID无效");
    }
    
    /**
     * 创建API调用频率限制异常
     */
    public static WeChatAuthException apiRateLimit() {
        return new WeChatAuthException("45011", "api rate limit", "微信API调用频率超出限制，请稍后重试");
    }
    
    /**
     * 创建网络连接错误异常
     */
    public static WeChatAuthException networkError(Throwable cause) {
        return new WeChatAuthException("50001", "network error", "微信API网络连接错误", cause);
    }
    
    /**
     * 创建配置错误异常
     */
    public static WeChatAuthException configError(String message) {
        return new WeChatAuthException("50002", "config error", "微信应用配置错误: " + message);
    }
    
    /**
     * 创建用户取消授权异常
     */
    public static WeChatAuthException userCancelled() {
        return new WeChatAuthException("用户取消了微信授权");
    }
    
    /**
     * 创建状态参数验证失败异常
     */
    public static WeChatAuthException invalidState() {
        return new WeChatAuthException("状态参数验证失败，可能存在CSRF攻击");
    }
    
    /**
     * 创建用户信息获取失败异常
     */
    public static WeChatAuthException userInfoFailed(String reason) {
        return new WeChatAuthException("获取微信用户信息失败: " + reason);
    }
    
    /**
     * 根据微信错误码创建对应异常
     */
    public static WeChatAuthException fromWeChatError(String errorCode, String errorMsg) {
        String message;
        switch (errorCode) {
            case "40001":
                message = "微信访问令牌无效或已过期";
                break;
            case "40003":
                message = "微信OpenID无效";
                break;
            case "40013":
                message = "微信AppID无效";
                break;
            case "40029":
                message = "微信授权码无效";
                break;
            case "40030":
                message = "微信授权码已过期";
                break;
            case "45011":
                message = "微信API调用频率超出限制";
                break;
            case "50001":
                message = "微信API内部错误";
                break;
            default:
                message = "微信认证失败: " + (errorMsg != null ? errorMsg : "未知错误");
        }
        return new WeChatAuthException(errorCode, errorMsg, message);
    }
}